//
//  ViewController.m
//  001--RAC集合类
//
//  Created by H on 2017/4/21.
//  Copyright © 2017年 TZ. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveObjC.h>
#import "KFC.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //RACSequence:用于代替NSArray,NSDictionary,可以使用快速的遍历
    // 最常见的应用场景:字典转模型!!
    //解析plist文件
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"kfc.plist" ofType:nil];
    NSArray * dictArr = [NSArray arrayWithContentsOfFile:filePath];
    
//    NSMutableArray * arr = [NSMutableArray array];
//    [dictArr.rac_sequence.signal subscribeNext:^(NSDictionary * x) {
//        KFC * kfc = [KFC kfcWithDict:x];
//        [arr addObject:kfc];
//    }];
    //更加爽的用法
    //会将一个集合中的所有元素都映射成一个新的对象!
    NSArray * arr = [[dictArr.rac_sequence map:^id _Nullable(NSDictionary * value) {
        //返回模型!!
        return  [KFC kfcWithDict:value];
    }] array];
    NSLog(@"%@",arr);
}

-(void)demo3{
    //字典
    NSDictionary * dict = @{@"name":@"hank",@"age":@"18"};
    
    //字典转集合
    [dict.rac_sequence.signal subscribeNext:^(RACTuple* x) {
        //        NSString * key = x[0];
        //        NSString * value = x[1];
        //        NSLog(@"%@%@",key,value);
        //RACTupleUnpack:用来解析元祖
        //宏里面的参数,传需要解析出来的变量名称
        // = 右边,放需要解析的元祖
        RACTupleUnpack(NSString * key,NSString * value) = x;
        NSLog(@"%@ : %@",key,value);
    }];
    

}

-(void)demo2{
    //数组
    NSArray * arr = @[@"abc",@"bbb",@123];
    
    
    //RAC集合
    //    RACSequence * requence = [arr rac_sequence];
    //    //遍历
    //    RACSignal * signal = [requence signal];
    //
    //    //订阅信号
    //    [signal subscribeNext:^(id  _Nullable x) {
    //        NSLog(@"%@",x);
    //    }];
    
    [arr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}

-(void)demo1{
    //元祖
    RACTuple * tuple = [RACTuple tupleWithObjectsFromArray:@[@"aaa",@"bbb",@123]];
    NSString * str = tuple[0];
    NSLog(@"%@",str);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
